package com.demo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.demo.common.vo.PageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.entity.User;
import com.demo.service.IUserService;
import com.demo.common.dto.PageDTO;
import com.demo.dto.UserDTO;
import com.demo.query.UserQueryDTO;
import com.demo.vo.UserVO;

import java.util.List;

@Tag(name = "用户表管理")
@RestController
@RequestMapping("/user")
public class UserController {

    private final IUserService userService;

    public UserController(IUserService userService) {
        this.userService = userService;
    }

    @SaCheckPermission("btn.queryUser")
    @Operation(summary = "查询所有用户表信息")
    @GetMapping("list")
    public SaResult list() {
        List<User> userList = userService.list();
        List<UserVO> result = BeanUtil.copyToList(userList, UserVO.class);
        return SaResult.ok().setData(result);
    }

    @SaCheckPermission("btn.queryUser")
    @Operation(summary = "分页查询用户表信息")
    @PostMapping("page")
    public SaResult page(@RequestBody UserQueryDTO userQueryDTO) {
        Integer currentPage = userQueryDTO.getCurrentPage();
        Integer pageSize = userQueryDTO.getPageSize();

        LambdaQueryWrapper<User> queryWrapper = userQueryDTO.buildQueryWrapper();
        Page<User> userPage = userService.page(new Page<>(currentPage, pageSize), queryWrapper);

        PageVO<UserVO> userPageVO = PageDTO.getPageVO(userPage, item -> BeanUtil.copyProperties(item, UserVO.class));
        return SaResult.ok().setData(userPageVO);
    }

    @SaCheckPermission(value = {"btn.addUser", "btn.editUser"}, mode = SaMode.OR)
    @Operation(summary = "新增/修改用户表信息")
    @PostMapping("createOrEdit")
    public SaResult createOrEdit(@RequestBody UserDTO userDTO) {
        User user = BeanUtil.copyProperties(userDTO, User.class);
        boolean result = userService.saveOrUpdate(user);
        return SaResult.ok().setData(result);
    }

    @SaCheckPermission("btn.queryUser")
    @Operation(summary = "根据id获取用户表信息")
    @GetMapping("getById/{id}")
    public SaResult getById(@Parameter(description = "用户表id")
                            @PathVariable Integer id) {
        User user = userService.getById(id);
        if (user == null) {
            return SaResult.error("数据不存在");
        }
        UserVO result = BeanUtil.copyProperties(user, UserVO.class);
        return SaResult.ok().setData(result);
    }

    @SaCheckPermission("btn.deleteUser")
    @Operation(summary = "根据id删除用户表信息")
    @DeleteMapping("remove/{id}")
    public SaResult remove(@Parameter(description = "用户表id")
                           @PathVariable Integer id) {
        boolean result = userService.removeById(id);
        return SaResult.ok(result ? "删除成功" : "删除失败").setData(result);
    }

    @SaCheckPermission("btn.deleteUser")
    @Operation(summary = "根据id批量删除用户表信息")
    @DeleteMapping("/removeBatch/{ids}")
    public SaResult removeBatch(@Parameter(description = "id集合")
                                @PathVariable List<Integer> ids) {
        boolean result = userService.removeByIds(ids);
        return SaResult.ok(result ? "删除成功" : "删除失败").setData(result);
    }
}
